package com.tfleo.mealssenger;

import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.adapter.NewsListAdapter;
import com.tfleo.mealssenger.service.ChatService;
import com.tfleo.mealssenger.utility.DataFetcher;

import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class Main extends TabActivity {
	private TabHost mTabHost;
	private TabSpec mTabSpec;
	private Intent mIntent;
	
	public static final String TAB_POS = "tab";
	
	public static final String APP_DATA = "appData";
	public static final String USER_DATA = "userData";
	
	public static final String RECEIVE_NOTIF = "receiveNotif";
	public static final String UPDATE_LOCATION = "updateLocation";
	public static final String REMEMBER_LOGIN = "rememberLogin";
	
	public static final String USER_ID = "userId";
	public static final String USER_NAME = "userName";
	public static final String USER_IMAGE = "userImage";
	
	public static final String LAST_CHECKIN_PLACEID = "checkinPlaceId";
	public static final String LAST_CHECKIN_PLACENAME = "checkinPlaceName";
	public static final String LAST_CHECKIN_ID = "checkinId";
	public static final String LAST_MEALUP_ID = "mealupId";
	public static final String TOKEN = "token";
	public static final String EMAIL = "email";
	
	public static final String LAST_LATITUDE = "lastLatitude";
	public static final String LAST_LONGITUDE = "lastLongitude";
	
	public static final String CHAT_ID = "chatId";
	public static final String ONLINE = "online"; //chatting service is running, user online
			
	public static final String SERVER_URL = "http://ec2-122-248-235-173.ap-southeast-1.compute.amazonaws.com/mealssenger/";
	
	private SharedPreferences mSharedPref;
	private SharedPreferences.Editor mPrefEditor;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mTabHost = getTabHost();
        int curTab = 1;
        
        mSharedPref = getSharedPreferences(Main.USER_DATA, MODE_PRIVATE);
        mPrefEditor = mSharedPref.edit();
        
        Bundle extras = getIntent().getExtras();
        if(extras != null) {
        	curTab = extras.getInt(TAB_POS);
        }
        mIntent = new Intent().setClass(this, MapBrowseActivity.class);
        setupTab(new TextView(this), "Map", R.drawable.ic_tab_map);
        
        mIntent = new Intent().setClass(this, GalleryActivity.class);
        setupTab(new TextView(this), "Gallery", R.drawable.ic_tab_nearby);
        
        mIntent = new Intent().setClass(this, NewsActivity.class);
        setupTab(new TextView(this), "Social", R.drawable.ic_tab_friend);
        
        if(curTab > 2 || curTab < 0) {
        	curTab = 1;
        }
        mTabHost.setCurrentTab(curTab); 
        
        StartChatTask task = new StartChatTask();
        task.execute();
    }
    
    @Override
    public void onDestroy() {
    	if(mSharedPref.getBoolean(Main.REMEMBER_LOGIN, false)) {
    		//do nothing
    	} else {
	    	if(mSharedPref.getBoolean(Main.ONLINE, false)) {
				mPrefEditor.putBoolean(Main.ONLINE, false);
				mPrefEditor.commit();
				ChatService.actionStop(getApplicationContext());
			}
    	}
    	super.onDestroy();
    }
    /**
     * Setup each tab specification
     * 
     * @param view
     * @param tag
     * @param icon
     */
    private void setupTab(View view, String tag, int icon) {
    	View tabview = createTabView(mTabHost.getContext(), tag, icon);
    	mTabSpec = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(mIntent);
    	mTabHost.addTab(mTabSpec);
    }
    
    /**
     * Setup view on tab bar
     * 
     * @param context
     * @param text
     * @param icon
     * @return View to be shown on tab bar
     */
    private static View createTabView(Context context, String text, int icon) {
    	View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    	
    	//TextView tv = (TextView) view.findViewById(R.id.tabs_text);
    	//tv.setText(text);
    	
    	ImageView iv = (ImageView) view.findViewById(R.id.tabs_icon);
    	iv.setImageResource(icon);
    	
    	return view;
    }
    
    private class StartChatTask extends AsyncTask<Void, Void, Void> {	
		@Override
		protected Void doInBackground(Void... voids) {
			if(mSharedPref.getBoolean(Main.ONLINE, false)) {
				ChatService.actionStart(getApplicationContext());
			}
			
			return null;
		}
	}
    
}